﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DemoForSnake
{
    public partial class FrmMain : Form
    {
        private Map map;
        private int gradeNum;
        public FrmMain()
        {
            InitializeComponent();
        }
        private void gameArea_Load(object sender, EventArgs e)
        {
            BackColor = Color.Silver;
        }
        private void gameArea_Paint(object sender, PaintEventArgs e)
        {
            if (map != null)//游戏已经初始化了
            {
                map.ShowMap(e.Graphics, this.gameArea.Size);
            }
        }
        //键盘响应事件
        private void gamrArea_KeyDown(object sender, KeyEventArgs e)
        {
            Keys k;
            int d = 0;
            k = e.KeyCode;
            switch (k)
            {
                //左A
                case Keys.A:
                    d = 3;
                    break;
                //右D
                case Keys.D:
                    d = 1;
                    break;
                //上W
                case Keys.W:
                    d = 0;
                    break;
                //下S
                case Keys.S:
                    d = 2;
                    break;

            }

            map.Snake.TurnDirection(d);
        }
        private void timer_Tick(object sender, EventArgs e)
        {
            开始ToolStripMenuItem.Text = "重新开始";
            lblScore.Text = map.score.ToString();
            Bitmap bmp = new Bitmap(Width, Height);
            BackgroundImage = bmp;
            Graphics g = Graphics.FromImage(bmp);
            map.ShowMap(g, this.gameArea.Size);
            map.HandleTouch();
            if (map.score >= 50)
            {
                timer.Enabled = false;
                MessageBox.Show("恭喜您，成功了！");
            }
            if (map.CheckSnake() || map.Snake.IsTouchMyself())
            {
                timer.Enabled = false;
                MessageBox.Show("很遗憾，失败了！");
                gameArea.Invalidate();
                map.Reset(CreateGraphics());
                map.score = 0;
                lblGrade.Text = "";
            }
            if (map.score < -10)
            {
                timer.Enabled = false;
                MessageBox.Show("很遗憾，失败了！");
                gameArea.Invalidate();
                map.Reset(CreateGraphics());
                map.score = 0;
                lblGrade.Text = "";
            }

        }
       

        private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //重置游戏
            map = new Map(new Point(0, 0));
            this.gameArea.Invalidate();
            if (lblGrade.Text == "")
            {
                MessageBox.Show("请先选择级别!");
                return;
            }
            if (开始ToolStripMenuItem.Text == "开始")
            {
                开始ToolStripMenuItem.Text = "重新开始";
                timer.Enabled = true;
            }
            else if (开始ToolStripMenuItem.Text == "重新开始")
            {
                map.Reset(CreateGraphics());
                map.score = 0;
                开始ToolStripMenuItem.Text = "开始";
                继续ToolStripMenuItem.Text = "暂停";
                timer.Enabled = false;
                if (lblGrade.Text == "")
                {
                    MessageBox.Show("请先选择级别!");
                    return;
                }
                timer.Enabled = true;
            }

        }

        private void 继续ToolStripMenuItem_Click(object sender, EventArgs e)
        {
                if (继续ToolStripMenuItem.Text == "暂停")
            {
                继续ToolStripMenuItem.Text = "继续";
                timer.Enabled = false;
            }
            else if (继续ToolStripMenuItem.Text == "继续")
            {
                继续ToolStripMenuItem.Text = "暂停";
                timer.Enabled = true;
            }
        }

        private void 低级ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gradeNum = 400;
            lblGrade.Text = "低级";
            timer.Interval = gradeNum;
        }

        private void 中级ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gradeNum = 200;
            lblGrade.Text = "中级";
            timer.Interval = gradeNum;
        }

        private void 高级ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gradeNum = 100;
            lblGrade.Text = "高级";
            timer.Interval = gradeNum;
        }

        private void 顶级ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gradeNum = 50;
            lblGrade.Text = "顶级";
            timer.Interval = gradeNum;
        }
    }

}

